package com.liangtian.mdm.system.parameter.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.liangtian.mdm.base.controller.BaseController;
import com.liangtian.mdm.base.utils.ResultObject;
import com.liangtian.mdm.dm.common.pojo.ResponseMsg;
import com.liangtian.mdm.system.log.service.SysLogService;
import com.liangtian.mdm.system.parameter.model.SysParameter;
import com.liangtian.mdm.system.parameter.service.SysParameterService;
import com.liangtian.mdm.util.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Title: 系统参数控制类</p>
 * <p>Description: </p>
 * <p>Company: 石化盈科信息技术有限责任公司 </p>
 * <p>Encoding    :  UTF-8</p>
 * <p>Package Name:  com.liangtian.mdm.system.parameter.controller </p>
 * <p>Project Name:  dmodelLoc </p>
 *
 * @author xiuzhangl
 * @date 2017年2月28日 上午10:24:54
 */

@Controller
@RequestMapping("/sys_param")
public class SysParameterController extends BaseController {

    // Session中保存的当前登录用户的信息
    //@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    @Autowired
    private SysLogService sysLogService;


    @Autowired
    private SysParameterService sysParameterService;

    /**
     * 系统参数列表
     *
     * @param request
     * @param dispatchTarget
     * @return
     * @author Administrator
     * @date 2017年2月28日 上午10:34:50
     */
    @RequestMapping(value = "/param_list", method = RequestMethod.POST)
    @ResponseBody
    @SuppressWarnings("rawtypes")
    public JSONObject param_list(HttpServletRequest request,
                                 SysParameter sysParameter) {


        Map paramMap = RequestUtils.paramToMap(request, SysParameter.class,
                false);

        PageInfo pageInfo = sysParameterService.queryPage(paramMap);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", pageInfo.getList());// JSONArray
        jsonObject.put("total", pageInfo.getTotal());//
        return jsonObject;
    }

    /**
     * 添加系统参数
     *
     * @param request
     * @param sysParameter
     * @return
     * @author xiuzhangl
     * @date 2017年3月1日 上午10:08:52
     */
    @RequestMapping(value = "/sysParam_add", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject saveDispatchTarget(HttpServletRequest request,
                                         SysParameter sysParameter) {

        JSONObject jsonObject = new JSONObject();
        ResponseMsg<String> rMsg = new ResponseMsg<String>();
        try {
            int count = sysParameterService
                    .addSysParameter(sysParameter);
            rMsg.setRspCode(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject.put("rmsg", rMsg);
        return jsonObject;

    }

    /**
     * 编辑系统参数
     *
     * @param request
     * @param sysParameter
     * @return
     * @author xiuzhangl
     * @date 2017年3月1日 下午2:29:42
     */
    @RequestMapping(value = "/sysParam_edit", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject editSysParam(HttpServletRequest request,
                                   SysParameter sysParameter) {

        JSONObject jsonObject = new JSONObject();
        ResponseMsg<String> rMsg = new ResponseMsg<String>();
        try {
            int count = sysParameterService
                    .editSysParam(sysParameter);
            rMsg.setRspCode(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject.put("rmsg", rMsg);
        return jsonObject;

    }

    /**
     * 系统参数批量删除
     *
     * @return 2017年2月15日 Administrator
     */
    @RequestMapping(value = "/sysParam_del", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject delSysParamByIds(HttpServletRequest request) {

        String ids = request.getParameter("ids");
        if (ids == null || "".equals(ids))
            return null;
        Map<String, Object> map = new HashMap<>();
        map.put("idList", ids.split(","));
        int ret = sysParameterService.delSysParamByIds(map);
        int f = ret > 0 ? 1 : 0;
        ResultObject result = new ResultObject(f, "删除成功", "删除失败");
        return result;
    }

    /**
     * 冻结活动
     *
     * @return
     */
    @RequestMapping(value = "/sysParam_u_status", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject updateSysParamStatus(HttpServletRequest request) {

        String status = request.getParameter("status");
        String ids = request.getParameter("ids");
        if (ids == null || "".equals(ids))
            return null;
        Map<String, Object> map = new HashMap<>();
        map.put("idList", ids.split(","));
        map.put("status", status);
        int ret = sysParameterService.updateSysParamStatus(map);
        int f = ret > 0 ? 1 : 0;
        ResultObject result = new ResultObject(f, "操作成功", "操作失败");
        return result;
    }

    /**
     * 根据id获取系统参数
     *
     * @param sysParameter
     * @return
     * @author xiuzhangl
     * @date 2017年3月1日 下午2:24:14
     */
    @ResponseBody
    @RequestMapping(value = "/getSysParamById", method = RequestMethod.POST)
    public String getSysParamById(SysParameter sysParameter) {
        SysParameter sysParam = null;
        try {
            sysParam = sysParameterService.getSysParamById(sysParameter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.toJSONString(sysParam);
    }


}
